package com.lindroy.androidplayer.ui.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.lindroy.androidplayer.R
import com.lindroy.lib_utils.extensions.screenWidth
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.dialog_loading.*

/**
 * @author Lin
 * @date 2021/4/11
 * @function 加载对话框
 */
class LoadingDialog : DialogFragment() {

    private lateinit var fm: FragmentManager

    private var loadingMsg = ""

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.dialog_loading, null)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tvLoading.text = loadingMsg
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            val params = attributes
            params.gravity = Gravity.CENTER
            //去除白色的背景
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            //设置窗体动画
//            setWindowAnimations(animStyle)
            params.width = (screenWidth * 0.35).toInt()
            attributes = params
        }
    }

    fun show(msg:String ="loading" ) {
        loadingMsg = msg
        show(fm, msg)
    }

    companion object {
        fun build(fragment: Fragment) = LoadingDialog().apply {
            fm = fragment.childFragmentManager
        }

        fun build(activity: FragmentActivity) = LoadingDialog().apply {
            fm = activity.supportFragmentManager
        }
    }
}